#include "WNPlatform/inc/WNSurfaceWindows.h"

using namespace WNPlatform;

WNSurfaceWindows::WNSurfaceWindows(WNSurfaceManagerWindows& _manager) :
    WNSurface(),
    mSurfaceManager(_manager),
    mNativeWindowHandle(NULL),
    mWidth(0),
    mHeight(0) {
}

WN_VOID WNSurfaceWindows::SetNativeWindowHandle(HWND _handle) {
    mNativeWindowHandle = _handle;
}

WNNativeGraphicsHandle WNSurfaceWindows::GetNativeGraphicsHandle() {
    return(mNativeWindowHandle);
}

WN_VOID WNSurfaceWindows::ProcessCallback(UINT _msg, LPARAM _lparam, WPARAM _wparam) {
    switch(_msg) {
    case WM_SIZE:
        mWidth = _lparam & 0x0000FFFF;
        mHeight = (_lparam >> 16) & 0x0000FFFF;
        FireCallback(WNSurface::eSETResize);
        break;
    }
}

HWND WNSurfaceWindows::GetNativeWindowHandle() {
    return(mNativeWindowHandle);
}


WNSurface::WNSurfaceError WNSurfaceWindows::Resize(WN_UINT32 _width, WN_UINT32 _height) {
    if(mNativeWindowHandle != NULL) {
        RECT rcClient, rcWind;
        POINT ptDiff;
        GetClientRect(mNativeWindowHandle, &rcClient);
        GetWindowRect(mNativeWindowHandle, &rcWind);
        ptDiff.x = (rcWind.right - rcWind.left) - rcClient.right;
        ptDiff.y = (rcWind.bottom - rcWind.top) - rcClient.bottom;
        MoveWindow(mNativeWindowHandle, 0, 0, _width + ptDiff.x, _height + ptDiff.y, true);  
    } else {
        mWidth = _width;
        mHeight = _height;
    }
    return(eWNOK);
}

WNSurface::WNSurfaceError WNSurfaceWindows::Move(WN_UINT32 _x, WN_UINT32 _y) {
    return(eWNOK);
}

WN_BOOL WNSurfaceWindows::IsFullscreen() {
    return(WN_FALSE);
}

WNSurface::WNSurfaceError WNSurfaceWindows::SetFullscreen(bool _fs) {
    return(eWNOK);
}

WN_UINT32 WNSurfaceWindows::GetWidth() const {
    return(mWidth);
}

WN_UINT32 WNSurfaceWindows::GetHeight() const {
    return(mHeight);
}

WN_UINT32 WNSurfaceWindows::GetX() const {
    return(0);
}

WN_UINT32 WNSurfaceWindows::GetY() const {
    return(0);
}
